package com.group3.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.group3.exception.BusinessException;
import com.group3.pojo.PunchRecordExcel;

import java.io.File;
import java.util.List;

/**
 * GenerateExcel
 *
 * @author HY
 * 2021/07/07 13:53
 **/


public class GenerateExcelUtil {


    public static boolean GenerateExcel(List list){
        String fileName = System.getProperty("user.dir") + "/file/ClockInTheTemplate/Data/打卡数据.xlsx";
        // 这里 需要指定写用哪个class去写
        ExcelWriter excelWriter = null;
        try {
            File file = new File(fileName);
//            判断文件是否存在,不存在则创建
            if (!file.exists()) {
//                判断文件夹(路径)是否存在,不存在则创建
                File fileParent = file.getParentFile();
                if(!fileParent.exists()) {
                    fileParent.mkdirs();
                }
                file.createNewFile();
            }
            excelWriter = EasyExcel.write(fileName, PunchRecordExcel.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(list, writeSheet);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(e);
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        return true;
    }
}
